<template>
  <div class="login">
    <el-card class="content">
      <div class="login-title">博客后台管理系统</div>
      <el-form :model="user">
        <el-form-item>
          <el-input
            v-model="user.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="user.pwd"
            type="password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item class="buttonBox">
          <span @click="$router.push('/reg')">还没有账号？点击注册</span>
          <el-button type="primary" size="mini" @click="getUserLogin"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { getUserLogin} from "@/api/user.js";
export default {
  data() {
    return {
      user: {
        username: "",
        pwd: "",
      },
    };
  },

  methods: {
    async getUserLogin() {
      const {data} = await getUserLogin({
        username: this.user.username,
        pwd: this.user.pwd,
      });
      const {token} =data
       const user = data.rel
      if (data.code == 200) {
        this.$message({
          message: "登陆成功",
          type:'success'
        });
        // 将用户token存储在vuex中
        this.$store.commit("setUserToken", token);
        //将用户信息存储在vuex中
         this.$store.commit("setUserInfo", user);
       setTimeout(() => {
          this.$router.push('/')
       }, 500);
      }else{
         this.$message({
          message: data.msg,
          type:'error'
        });
      }
    },
  },
};
</script>

<style lang="less" scoped>
.login {
  height: 100%;
  width: 100%;
  background: url("../../assets/loginBgi.png") no-repeat;
  background-size: 100% 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .content {
    width: 390px;
    height: 260px;

    .login-title {
      text-align: center;
      font-weight: 800;
      margin-bottom: 30px;
      font-size: 19px;
    }
    .buttonBox {
      display: flex;
      justify-content: flex-end;
      span {
        font-size: 13px;
        margin-right: 20px;
      }
      span:hover {
        color: rgb(145, 141, 141);
        cursor: pointer;
      }
    }
  }
}
</style>